﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static string SHA256(string str)
        {
            //如果str有中文，不同Encoding的sha是不同的！！
            byte[] SHA256Data = Encoding.UTF8.GetBytes(str);

            SHA256Managed Sha256 = new SHA256Managed();
            byte[] by = Sha256.ComputeHash(SHA256Data);

            return BitConverter.ToString(by).Replace("-", "").ToLower(); //64
            //return Convert.ToBase64String(by);                         //44
        }

        public string SHA256Encrypt(string strIN)
        {
            //string strIN = getstrIN(strIN);
            byte[] tmpByte;
            SHA256 sha256 = new SHA256Managed();

            byte[] SHA256Data = Encoding.UTF8.GetBytes(strIN);
            //tmpByte = sha256.ComputeHash(GetKeyByteArray(strIN));

            tmpByte = sha256.ComputeHash(SHA256Data);
            sha256.Clear();

            return GetStringValue(tmpByte);

        }

        private string GetStringValue(byte[] Byte)
        {
            string tmpString = "";
            //if (this.isReturnNum == false)
            //{
            //    ASCIIEncoding Asc = new ASCIIEncoding();
            //    tmpString = Asc.GetString(Byte);
            //}
            //else
            //{
            //    int iCounter;
            //    for (iCounter = 0; iCounter < Byte.Length; iCounter++)
            //    {
            //        tmpString = tmpString + Byte[iCounter].ToString();
            //    }
            //}

            UTF8Encoding ecd = new UTF8Encoding();
            tmpString = ecd.GetString(Byte);

            return tmpString;
        }

        private byte[] GetKeyByteArray(string strKey)
        {
            UTF8Encoding ecd = new UTF8Encoding();
            //ASCIIEncoding Asc = new ASCIIEncoding();

            int tmpStrLen = strKey.Length;
            byte[] tmpByte = new byte[tmpStrLen - 1];

            tmpByte = ecd.GetBytes(strKey);

            return tmpByte;

        }

        static double ToTimestamp(DateTime value)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
            return (long)(value - startTime).TotalSeconds;
        }

        public static int ConvertDateTimeInt(System.DateTime time)
        {
            DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);
            return Convert.ToInt32((DateTime.Now - dateStart).TotalSeconds);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var t = textBox1.Text.Trim();

            var timestamp = ToTimestamp(DateTime.Now);
            var ss = $"dafa72f2-b830-4966-a178-70809168a452-{timestamp}-9ddb06d8-68bb-4805-babe-d843ccec1978";
            var sign = SHA256(ss);

            textBox2.Text += "timestamp:" + timestamp.ToString();
            textBox2.Text += "sign:" + sign;

            MessageBox.Show("ok!");
            return;

            List<PersonT> data = GetPopulatedData();

            var totals = data.GroupBy(x =>
                new { x.Name, x.City, x.ZipCode }).Select(y =>
                    y.Sum(i => i.SomeValue)).ToList();

            var groupsForIterate = data.GroupBy(x =>
                new { x.Name, x.City, x.ZipCode }).ToList();

            var totals1 = data.Where(x => x.Name == "Mark1").GroupBy(x =>
                new {x.Name})
                .Select(
                    y =>
                        new
                        {
                            Name = y.Key.Name,
                            Count = y.Count(),
                            T1 = y.Sum(p => p.SomeValue),
                            t2 = y.Sum(p => p.Value1)
                        }).ToList();

            foreach (var total in totals1)
            {
                Console.WriteLine(total.ToString());
            }
        }

        private static List<PersonT> GetPopulatedData()
        {
            List<PersonT> datos = new List<PersonT>()
            {
                new PersonT()
                {
                    Name = "Mark",
                    City = "BCN",
                    ZipCode = "00000",
                    SomeValue = 1,
                    Value1 = 2
                }, // group A

                new PersonT()
                {
                    Name = "Mark",
                    City = "BCN",
                    ZipCode = "000000",
                    SomeValue = 1,
                    Value1 = 2
                }, // group B

                new PersonT()
                {
                    Name = "Mark",
                    City = "BCN",
                    ZipCode = "000000",
                    SomeValue = 1,
                    Value1 = 2
                }, // group B

                new PersonT()
                {
                    Name = "John",
                    City = "NYC",
                    ZipCode = "000000",
                    SomeValue = 1,
                    Value1 = 2
                }, // group C
            };
            return datos;
        }
    }

    public class PersonT
    {
        public string Name { get; set; }
        public string City { get; set; }
        public string ZipCode { get; set; }
        public int SomeValue { get; set; }
        public int Value1 { get; set; }

    }
}
